package com.google.product.listener;

import com.alibaba.fastjson.JSON;
import com.google.product.config.ProductElasticSearchConfig;
import com.google.product.entity.ProductEntity;
import com.google.product.service.GoodsService;
import com.rabbitmq.client.Channel;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import com.google.product.entity.GoodsEntity;

import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

@Component
public class MyListener {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    RestHighLevelClient client;//测试es官方客户端

    @RabbitListener(queues = "goods-save-mysql")
    public void getMessage(GoodsEntity msg, Channel channel, Message message) throws IOException {
//        System.out.println("mysql");
//        System.out.println(msg);
        goodsService.save(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "goods-save-es")
    public void getMessage2(GoodsEntity msg, Channel channel, Message message) throws IOException {
//        System.out.println("es");
//        System.out.println(msg);


        //指定索引
        IndexRequest indexRequest = new IndexRequest("store");
        //指定ID 如果不设置自动生成
        indexRequest.id(msg.getGoodsId()+"");
        //1、使用kv结构
        //index.source("userName","daihao","age","18");

        //2、使用json

        String json = JSON.toJSONString(msg);

        indexRequest.source(json, XContentType.JSON);// 保存的内容

        //执行同步存储 RequestOptions.DEFAULT:请求设置项
//        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        IndexResponse index = client.index(indexRequest, ProductElasticSearchConfig.COMMON_OPTIONS);


        //提取有用得响应数据
        System.out.println(index);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "goods-update-mysql")
    public void getMessage3(GoodsEntity msg, Channel channel, Message message) throws IOException {
//        System.out.println("mysql");
//        System.out.println(msg);
        goodsService.updateById(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "goods-update-es")
    public void getMessage4(GoodsEntity msg, Channel channel, Message message) throws IOException {
//        System.out.println("es");
//        System.out.println(msg);


        //指定索引
        IndexRequest indexRequest = new IndexRequest("store");
        //指定ID 如果不设置自动生成
        indexRequest.id(msg.getGoodsId()+"");
        //1、使用kv结构
        //index.source("userName","daihao","age","18");

        //2、使用json

        String json = JSON.toJSONString(msg);

        indexRequest.source(json, XContentType.JSON);// 保存的内容

        //执行同步存储 RequestOptions.DEFAULT:请求设置项
//        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        IndexResponse index = client.index(indexRequest, ProductElasticSearchConfig.COMMON_OPTIONS);


        //提取有用得响应数据
        System.out.println(index);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "goods-delete-mysql")
    public void getMessage5(Long[] msg, Channel channel, Message message) throws IOException {
//        System.out.println("mysql");
//        System.out.println(msg);
        goodsService.removeByIds(Arrays.asList(msg));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "goods-delete-es")
    public void getMessage6(Long[] msg, Channel channel, Message message) throws IOException {

        long id = msg[0];
        //1. 封装Request对象
        DeleteRequest request = new DeleteRequest("store",id+"");

        //2. client执行
        DeleteResponse resp = client.delete(request, RequestOptions.DEFAULT);

        //3. 输出结果
        System.out.println("es删除的响应");
        System.out.println(resp.getResult().toString());

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
